package cyy.example.com.facotry.net.helper;


import android.text.TextUtils;
import android.util.Log;

import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.AppUtils;
import com.example.com.common.utils.EncryptUtils;
import com.example.com.common.utils.L;
import com.example.com.common.utils.ListUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.request.CheckUserEntity;
import cyy.example.com.facotry.model.api.request.ClockedInEntity;
import cyy.example.com.facotry.model.api.request.EditUserInfoEntity;
import cyy.example.com.facotry.model.api.request.LoginEntity;
import cyy.example.com.facotry.model.api.request.UpLocationInfo;
import cyy.example.com.facotry.model.api.request.UserRegisterEntity;
import cyy.example.com.facotry.model.api.response.ConfigEntitySpare;
import cyy.example.com.facotry.model.api.response.FirmListEntity;
import cyy.example.com.facotry.model.api.response.MissionLocationEntity;
import cyy.example.com.facotry.model.api.response.SearchMenuEntity;
import cyy.example.com.facotry.model.api.response.StaffEntity;
import cyy.example.com.facotry.model.api.response.TaskDetailsInfoEntity;
import cyy.example.com.facotry.net.Network;
import cyy.example.com.facotry.net.RemoteService;
import cyy.example.com.facotry.net.RequestIdFactory;
import cyy.example.com.facotry.share.SPEngine;
import retrofit2.Call;
import retrofit2.Retrofit;

/**
 * Created by chenyiyao on 2017/10/25.
 */

public class NetHelper {


    /**
     * 登陆，异步的调用
     *
     * @param userId   传递一个注册的Model进来
     * @param password 登陆密码
     * @param callback 成功与失败的接口回送
     */
    public static void login(String userId, String password, DataSource.Callback callback) {
        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService remote = Network.remote();
        try {
//            String psw = EncryptUtils.encryptMD5ToString(password);

            LoginEntity entity = new LoginEntity(userId, password);
            Call login = remote.login(entity);
            login.enqueue(new RspCallback(callback, RequestIdFactory.USER_LOGIN));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 根据名字查询公司信息F
     *
     * @param name
     * @param callback
     */
    public static void queryClassfyFirmList(String name, DataSource.Callback<FirmListEntity> callback) {
        queryClassfyFirmList("", "", name, callback);
    }

    /**
     * /**
     * 查询公司列表
     *
     * @param callback
     */
    public static void queryClassfyFirmList(String pageSize, String pageNum, String name, DataSource.Callback<FirmListEntity> callback) {
        queryClassfyFirmList(pageSize, pageNum, null, null, null, null, null, name, callback);
    }

    /**
     * @param pageNum           分页
     * @param companyName       公司名
     * @param areaName          区域名
     * @param industryId        行业类型
     * @param superviseNatureId 监管性质
     * @param superviseGradeId  监管等级
     * @param illegalRecordId   违法记录
     * @param callback
     */
    public static void queryClassfyFirmList(String pageSize, String pageNum, List<String> areaName, List<String> industryId,
                                            String superviseNatureId, String superviseGradeId,
                                            String illegalRecordId, String companyName, DataSource.Callback<FirmListEntity> callback) {
        HashMap<String, String> map = new HashMap<>();
        if (!TextUtils.isEmpty(pageNum)) {
            map.put("pageNum", pageNum);
        }
        if (!TextUtils.isEmpty(pageSize)) {
            map.put("pageSize", pageSize);
        }
        if (!TextUtils.isEmpty(superviseNatureId)) {
            map.put("superviseNatureId", superviseNatureId);
        }
        if (!TextUtils.isEmpty(superviseGradeId)) {
            map.put("superviseGradeId", superviseGradeId);
        }
        if (!TextUtils.isEmpty(illegalRecordId)) {
            map.put("illegalRecordId", illegalRecordId);
        }
        if (!TextUtils.isEmpty(companyName)) {
            map.put("companyName", companyName);
        }
        RemoteService remote = Network.remote();
        Call<FirmListEntity> rspModelCall = remote.queryClassfyFirmList(map, areaName, industryId);
        rspModelCall.enqueue(new RspCallback(callback, RequestIdFactory.QUERY_FIRM_LIST_INFO));
    }


    /**
     * 上传我的位置
     *
     * @param callback
     */
    public static Call UpMyLocation(double latitude, double longitude, DataSource.Callback<BaseEntity> callback) {
        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService remote = Network.remote();
        // 得到一个Call
        // 异步的请求
        String user_id = SPEngine.getSPEngine().getUserInfo().getUser_id();
        UpLocationInfo info = new UpLocationInfo(user_id, latitude, longitude);
        Call<BaseEntity> call = remote.upMyLocation(info);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.UP_MY_LOCATION));
        return call;
    }

    /**
     * 地图显示的任务点列表
     *
     * @param callback
     */
    public static Call queryMapTaskInfoList(DataSource.Callback callback) {
        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService remote = Network.remote();
        // 得到一个Call
        // 异步的请求
        Call call = remote.queryMapTaskInfoList();
        call.enqueue(new RspCallback(callback, RequestIdFactory.QUERY_MAP_TASK));
        return call;
    }

    /**
     * 地图显示的人位置的列表
     *
     * @param callback
     */
    public static Call queryMapPeoleInfoList(DataSource.Callback callback) {
        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService remote = Network.remote();
        // 得到一个Call
        // 异步的请求
        String user_id = SPEngine.getSPEngine().getUserInfo().getUser_id();
        Call call = remote.queryMapPeoleInfoList();
        call.enqueue(new RspCallback(callback, RequestIdFactory.QUERY_MAP_PEOLE));
        return call;
    }

    /**
     * 地图显示的公司详细信息
     *
     * @param callback
     */
    public static Call getWarningFirmInfo(DataSource.Callback callback) {
        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService remote = Network.remote();
        // 得到一个Call
        // 异步的请求
        Call call = remote.getWarnFirm();
        call.enqueue(new RspCallback(callback, RequestIdFactory.GET_WARN_FIRM_LOCATION));
        return call;
    }

    /**
     * 更新app
     */
    public static Call checkAppVersion(DataSource.Callback callback) {
        RemoteService remoteService = Network.remote();
        int appVersionCode = AppUtils.getAppVersionCode();
        Call call = remoteService.upAPPVersion(1 + "", "" + appVersionCode);
        call.enqueue(new RspCallback(callback, RequestIdFactory.CHECK_APP_VERSION));
        return call;
    }


    /**
     * 获取打卡人的列表
     */
    public static Call getHaveTraceUser(DataSource.Callback callback) {
//        long l = System.currentTimeMillis();
//        RemoteService remote = Network.remote();
//        Call call = remote.getHaveTrackUser();
//        call.enqueue(new RspCallback(callback, RequestIdFactory.STAFF_LIST));
        return null;
    }


    /**
     * -----------------------------------------
     */

    public static Call getAPPConfig(DataSource.Callback<BaseEntity<ConfigEntitySpare>> callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getConfig();
        call.enqueue(new RspCallback(callback, RequestIdFactory.GET_CONFIG));
        return call;
    }


    /**
     * 获取筛选菜单数据
     *
     * @param callback
     * @return
     */
    public static Call getSearchMenuConfig(DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call<BaseEntity<List<SearchMenuEntity>>> call = remote.getSearchMenuConfig();
        call.enqueue(new RspCallback<BaseEntity<List<SearchMenuEntity>>>(callback, RequestIdFactory.GET_MENU_CONFIG));
        return call;
    }

    /**
     * 获取用户列表
     *
     * @param pageNum
     * @param callback
     * @return
     */
    public static Call getStaffList(int pageNum, int pageSize, List userType, List roleId, String status, DataSource.Callback callback) {
        return getStaffList(pageNum, pageSize, "", userType, roleId, status, callback);
    }

    /**
     * @param pageNum 页数
     * @param name    用户名
     *                搜索用户
     */

    /**
     * @param pageNum  页数
     * @param pageSize 用户名
     * @param name     搜索字段
     * @param userType 用户类型
     * @param roleId   橘色
     * @param status   用户状态 0通过审核  1 未通过
     * @param callback
     * @return
     */
    public static Call getStaffList(int pageNum, int pageSize, String name, List userType, List roleId, String status, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Map jsonMap = new HashMap();
        jsonMap.put("pageSize", pageSize);
        jsonMap.put("pageNum", pageNum);

        if (!TextUtils.isEmpty(name)) {
            jsonMap.put("username", name);
        }
        if (TextUtils.isEmpty(status)) {
            jsonMap.put("status", status);
        }
        if (!ListUtil.isEmpty(roleId)) {
//            jsonMap.put("roleId", roleId);
        }
        Call<BaseEntity<StaffEntity>> call = remote.getStaffList(jsonMap, userType, roleId, status);
        call.enqueue(new RspCallback<BaseEntity<StaffEntity>>(callback, RequestIdFactory.STAFF_LIST));
        return call;
    }

    /**
     * 查询用户详情
     *
     * @param id 用户id
     * @return
     */
    public static Call searchStaffDetails(String id, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getStaffDetails(id);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.QUERY_USER_DETAIL));
        return call;
    }


    /**
     * 打卡记录
     */
    public static Call UserClockIn(double latitude, double longitude, DataSource.Callback callback) {

        long l = System.currentTimeMillis();
        String user_id = SPEngine.getSPEngine().getUserInfo().getUser_id();
        RemoteService remote = Network.remote();
        //22.7526408758,113.8456696272
        ClockedInEntity entity = new ClockedInEntity(user_id, latitude, longitude, l);
        Call call = remote.UserClockIn(entity);
        call.enqueue(new RspCallback(callback, RequestIdFactory.CLOCK_IN));
        return call;
    }

    /**
     * 获取打卡地址
     */
    public static Call getUserTrack(DataSource.Callback callback, String user, long beginTime, long endTime) {
        RemoteService remote = Network.remote();
        Call call = remote.getUserTrack(user, beginTime, endTime);
        call.enqueue(new RspCallback(callback, RequestIdFactory.CLOCK_IN_LOCATION));
        return call;
    }

    /**
     * 获取打卡详情
     *
     * @param user      查询id
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @param pageSize  条数
     * @param pageNum   页码
     * @return
     */
    public static Call getUserTraceDet(DataSource.Callback callback, String user, String beginTime, String endTime, int pageSize, int pageNum) {
        RemoteService remote = Network.remote();
        Call call = remote.getUserTrackDet(user, beginTime, endTime, pageSize, pageNum);
        call.enqueue(new RspCallback<BaseEntity>(callback, 1));
        return call;
    }

    /**
     * 获取打卡记录的时间列表
     */
    public static Call getUserTrackDate(DataSource.Callback callback, String user) {
        RemoteService remote = Network.remote();
        Call call = remote.getUserTrackDate(user);
        call.enqueue(new RspCallback(callback, RequestIdFactory.CLOCK_IN_DATE));
        return call;
    }

    /**
     * 获取超标数据
     */
    public static Call getOverproofFirmDetails(String companyId, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getOverproofFirmDetails(companyId);
        call.enqueue(new RspCallback(callback, RequestIdFactory.GET_FIRM_OVER_DATA));
        return call;
    }

    public static Call getOverproofFirmDetail(String companyId, int type, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getOverproofFirmDetails(companyId);
        call.enqueue(new RspCallback(callback, type));
        return call;
    }

    /**
     * 模糊所搜含有关键字的公司，若不传则获取全部
     */
    public static Call getSearchFirmById(String condition, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getSearchFirmById(condition);
        call.enqueue(new RspCallback(callback, RequestIdFactory.SEARCH__FIRM_NAME_ID));
        return call;
    }

    /**
     *
     */
    public static Call getMyPublish(int pageSize, int pageNumpa, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        String user_account = SPEngine.getSPEngine().getUserInfo().getUser_account();
        Call call = remote.getMyPublishList(pageSize, pageNumpa, user_account);
        call.enqueue(new RspCallback(callback, RequestIdFactory.GET_MY_PUBLISH));
        return call;
    }

    /**
     * 修改用户信息
     *
     * @param realname 真实姓名
     * @param phone    电话
     * @param email    邮件
     */
    public static Call editUserInfo(String realname, String phone, String email, DataSource.Callback callback) {
        EditUserInfoEntity entity = new EditUserInfoEntity();
        EditUserInfoEntity.sysUserBean userBean = new EditUserInfoEntity.sysUserBean();
        entity.setSysUser(userBean);
        if (!TextUtils.isEmpty(realname)) {
            userBean.setRealname(realname);
        }
        if (!TextUtils.isEmpty(phone)) {
            userBean.setPhone(phone);
        }
        if (!TextUtils.isEmpty(email)) {
            userBean.setEmail(email);
        }
        userBean.setUserId(SPEngine.getSPEngine().getUserInfo().getUser_id());

        RemoteService remote = Network.remote();

        Call call = remote.editPersonalInfo(entity);
        call.enqueue(new RspCallback(callback, RequestIdFactory.EDIT_PERSONAL_INFO));
        return call;
    }

    /**
     * 获取轮播图数据
     */
    public static Call getBannerList(DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getBannerList();
        call.enqueue(new RspCallback(callback, RequestIdFactory.GET_BANNER_DATAS));
        return call;
    }

    /**
     * 查询用户详情
     *
     * @param idCard 身份证
     * @return
     */
    public static Call upUserIdCard(String userId, String idCard, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Map<String, String> map = new HashMap<>();
        map.put("userId", userId);
        map.put("param1", idCard);

        Call call = remote.upUserIdCard(map);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.QUERY_USER_DETAIL));
        return call;
    }

    public static Call userRegister(String username, String password, String regionCode, String realName,
                                    String phone, String email, String idCard, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        UserRegisterEntity entity = new UserRegisterEntity(username, password, regionCode, realName, phone, email, idCard);
        Call<BaseEntity> call = remote.userRegister(entity);
        call.enqueue(new RspCallback(callback, RequestIdFactory.USER_REGISTER));
        return call;
    }

    /**
     * 获取用户名及id
     *
     * @param username
     * @param callback
     * @return
     */
    public static Call getUserNameAndId(String username, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getUserIdAndName(username);
        call.enqueue(new RspCallback(callback, RequestIdFactory.USER_NAME_ID));
        return call;
    }


    /**
     * @param userId          用户id
     * @param userName        用户账号
     * @param realName        真实用户名
     * @param regionCode      所属地区码
     * @param phone           电话
     * @param email           邮箱
     * @param roleId          角色id
     * @param userType        用户类型
     * @param firm            所属单位
     * @param department      所属部门
     * @param superiorAccount 上级账号
     * @param preuserName    上级姓名
     * @param superiorId      上级id
     * @param job             岗位
     * @param duty            职责
     * @param team            所属中队
     * @param callback
     * @return
     */
    public static Call CheckUser(String userId, String userName, String realName, String regionCode, String phone,
                                 String email, String roleId, String userType, String firm, String department,
                                 String superiorAccount, String preuserName, String superiorId, String job, String duty, String team, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        if (TextUtils.isEmpty(userId) || TextUtils.isEmpty(roleId)) {
            return null;
        }

        CheckUserEntity username = new CheckUserEntity(userId, userName, realName, regionCode, phone, email, roleId, userType, firm, department, job, duty, superiorAccount, preuserName, superiorId, team);

        Call call = remote.checkUser(username);
        call.enqueue(new RspCallback(callback, RequestIdFactory.CHECK_USER));
        return call;
    }


    public static Call getWarnCount(DataSource.Callback callback){
        RemoteService remote = Network.remote();
        Call call = remote.getWarnCount();
        call.enqueue(new RspCallback(callback, RequestIdFactory.WARN_COUNT));
        return call;
    }

    public static Call getWifiWarnCount(DataSource.Callback callback){
        RemoteService remote = Network.remote();
        Log.e("+++++", "是单独发顺丰");
        Call call = remote.getWifiWarnCount();
        call.enqueue(new RspCallback(callback, RequestIdFactory.Wifi_WARN_COUNT_ONE));
        return call;
    }

    public static Call getQueryOfflineList(int pageNum, int pageSize, DataSource.Callback callback){
        RemoteService remote = Network.remote();
        Call call = remote.getQueryOfflineList(pageNum, pageSize);
        call.enqueue(new RspCallback(callback, RequestIdFactory.WARN_WIFI));
        return call;
    }
}
